package com.xueyi.aixihu.pattern;

import com.xueyi.aixihu.constant.TwoLevelAuditConstant;
import com.xueyi.aixihu.entity.po.AxhFinance;
import com.xueyi.aixihu.entity.vo.DtUsersVo;
import com.xueyi.aixihu.exception.SzException;
import com.xueyi.aixihu.status.SystemCode;

/**
 * 描述 报表审核流程
 * @author 刘梓江
 */
public interface FinanceReportAuditProcess {

    //提交
    default void submit(FinanceReportAuditProcessDirector director) throws SzException {
        throw new SzException(SystemCode.USER_OPERATION_WARNING, "流程操作异常");
    }

    //同意
    default void passed(FinanceReportAuditProcessDirector director) throws SzException {
        throw new SzException(SystemCode.USER_OPERATION_WARNING, "流程操作异常");
    }

    //驳回
    default void dismiss(String dismissDesc, FinanceReportAuditProcessDirector director) throws SzException {
        throw new SzException(SystemCode.USER_OPERATION_WARNING, "流程操作异常");
    }

    //发布
    default void release(FinanceReportAuditProcessDirector director) throws SzException {
        throw new SzException(SystemCode.USER_OPERATION_WARNING, "流程操作异常");
    }


}

//提交流程
class FinanceReportSubmitProcess implements FinanceReportAuditProcess {

    private FinanceReportSubmitProcess() {}
    private static final FinanceReportSubmitProcess instance = new FinanceReportSubmitProcess();
    public static FinanceReportSubmitProcess getInstance() {
        return instance;
    }

    @Override
    public void submit(FinanceReportAuditProcessDirector director) throws SzException {
        AxhFinance report = director.getReport();
        Integer currentReportState = report.getCurrentStatus();
        if (currentReportState.equals(TwoLevelAuditConstant.DRAFT) || currentReportState.equals(TwoLevelAuditConstant.ONE_FAIL)) {

            //获取当前用户角色
            Integer userMaxRole = DtUsersVo.findCurrentUser().getUserMaxRole();

            //判断当前报表，该当前用户是否有权利操作
            CheckOperationReportRoleStrategyDirector roleStrategyDirector = new CheckOperationReportRoleStrategyDirector();
            CheckOperationReportRoleStrategy roleStrategy = roleStrategyDirector.getRoleStrategy(director.getReportAreaType());
            if (!roleStrategy.submit(userMaxRole)) {
                throw new SzException(SystemCode.USER_OPERATION_WARNING, "当前角色权限，无法操作报表提交");
            }

            //将报表设置成提交
            report.setCurrentStatus(TwoLevelAuditConstant.SUBMIT_AUDIT);
        } else {
            throw new SzException(SystemCode.USER_OPERATION_WARNING, "当前报表状态，不在这个流程操作");
        }
    }
}

//一级审核流程
class FinanceReportOneLevelAuditProcess implements FinanceReportAuditProcess {

    private FinanceReportOneLevelAuditProcess() {}
    private static final FinanceReportOneLevelAuditProcess instance = new FinanceReportOneLevelAuditProcess();
    public static FinanceReportOneLevelAuditProcess getInstance() {
        return instance;
    }

    //同意
    public void passed(FinanceReportAuditProcessDirector director) throws SzException {
        AxhFinance report = director.getReport();
        Integer currentReportState = report.getCurrentStatus();
        if (TwoLevelAuditConstant.SUBMIT_AUDIT.equals(currentReportState) || TwoLevelAuditConstant.TWO_FAIL.equals(currentReportState)) {

            //获取当前用户角色
            Integer userMaxRole = DtUsersVo.findCurrentUser().getUserMaxRole();

            //获取类型报表角色校验策略对象
            CheckOperationReportRoleStrategyDirector roleStrategyDirector = new CheckOperationReportRoleStrategyDirector();
            CheckOperationReportRoleStrategy roleStrategy = roleStrategyDirector.getRoleStrategy(director.getReportAreaType());
            if (!roleStrategy.oneLevelAudit(userMaxRole)) {
                throw new SzException(SystemCode.USER_OPERATION_WARNING, "当前角色权限，无法操作报表审核");
            }

            //将报表设置一级同意
            report.setCurrentStatus(TwoLevelAuditConstant.ONE_CONSENT);

        } else {
            throw new SzException(SystemCode.USER_OPERATION_WARNING, "当前报表状态，不在这个流程操作");
        }

    }

    //驳回
    public void dismiss(String dismissDesc, FinanceReportAuditProcessDirector director) throws SzException {
        AxhFinance report = director.getReport();
        Integer currentReportState = report.getCurrentStatus();
        if (TwoLevelAuditConstant.SUBMIT_AUDIT.equals(currentReportState) || TwoLevelAuditConstant.TWO_FAIL.equals(currentReportState)) {

            //获取当前用户角色
            Integer userMaxRole = DtUsersVo.findCurrentUser().getUserMaxRole();

            //获取类型报表角色校验策略对象
            CheckOperationReportRoleStrategyDirector roleStrategyDirector = new CheckOperationReportRoleStrategyDirector();
            CheckOperationReportRoleStrategy roleStrategy = roleStrategyDirector.getRoleStrategy(director.getReportAreaType());
            if (!roleStrategy.oneLevelAudit(userMaxRole)) {
                throw new SzException(SystemCode.USER_OPERATION_WARNING, "当前角色权限，无法操作报表审核");
            }

            //将报表设置一级拒绝
            report.setCurrentStatus(TwoLevelAuditConstant.ONE_FAIL);
            report.setFbRejectDetail(dismissDesc);
        } else {
            throw new SzException(SystemCode.USER_OPERATION_WARNING, "当前报表状态，不在这个流程操作");
        }
    }
}


//二级审核流程
class FinanceReportTwoLevelAuditProcess implements FinanceReportAuditProcess {

    private FinanceReportTwoLevelAuditProcess() {}
    private static final FinanceReportTwoLevelAuditProcess instance = new FinanceReportTwoLevelAuditProcess();
    public static FinanceReportTwoLevelAuditProcess getInstance() {
        return instance;
    }

    //同意
    public void passed(FinanceReportAuditProcessDirector director) throws SzException {
        AxhFinance report = director.getReport();
        Integer currentReportState = report.getCurrentStatus();
        if (TwoLevelAuditConstant.ONE_CONSENT.equals(currentReportState)) {

            //获取当前用户角色
            Integer userMaxRole = DtUsersVo.findCurrentUser().getUserMaxRole();

            //获取类型报表角色校验策略对象
            CheckOperationReportRoleStrategyDirector roleStrategyDirector = new CheckOperationReportRoleStrategyDirector();
            CheckOperationReportRoleStrategy roleStrategy = roleStrategyDirector.getRoleStrategy(director.getReportAreaType());
            if (!roleStrategy.twoLevelAudit(userMaxRole)) {
                throw new SzException(SystemCode.USER_OPERATION_WARNING, "当前角色权限，无法操作报表审核");
            }

            //将报表设置二级同意
            report.setCurrentStatus(TwoLevelAuditConstant.TWO_CONSENT);

        } else {
            throw new SzException(SystemCode.USER_OPERATION_WARNING, "当前报表状态，不在这个流程操作");
        }

    }

    //驳回
    public void dismiss(String dismissDesc, FinanceReportAuditProcessDirector director) throws SzException {
        AxhFinance report = director.getReport();
        Integer currentReportState = report.getCurrentStatus();
        if (TwoLevelAuditConstant.ONE_CONSENT.equals(currentReportState)) {

            //获取当前用户角色
            Integer userMaxRole = DtUsersVo.findCurrentUser().getUserMaxRole();

            //获取类型报表角色校验策略对象
            CheckOperationReportRoleStrategyDirector roleStrategyDirector = new CheckOperationReportRoleStrategyDirector();
            CheckOperationReportRoleStrategy roleStrategy = roleStrategyDirector.getRoleStrategy(director.getReportAreaType());
            if (!roleStrategy.twoLevelAudit(userMaxRole)) {
                throw new SzException(SystemCode.USER_OPERATION_WARNING, "当前角色权限，无法操作报表审核");
            }

            //将报表设置二级拒绝
            report.setCurrentStatus(TwoLevelAuditConstant.TWO_FAIL);
            report.setFbRejectDetail(dismissDesc);
        } else {
            throw new SzException(SystemCode.USER_OPERATION_WARNING, "当前报表状态，不在这个流程操作");
        }
    }
}

//发布流程
class FinanceReportReleaseProcess implements FinanceReportAuditProcess{

    private FinanceReportReleaseProcess() {}
    private static final FinanceReportReleaseProcess instance = new FinanceReportReleaseProcess();
    public static FinanceReportReleaseProcess getInstance() {
        return instance;
    }

    //发布
    public void release(FinanceReportAuditProcessDirector director) throws SzException {
        AxhFinance report = director.getReport();
        //获取当前报表状态
        Integer currentReportState = report.getCurrentStatus();
        if(currentReportState.equals(TwoLevelAuditConstant.ONE_CONSENT)||currentReportState.equals(TwoLevelAuditConstant.TWO_CONSENT)){

            //获取当前用户角色
            Integer userMaxRole = DtUsersVo.findCurrentUser().getUserMaxRole();

            //获取类型报表角色校验策略对象
            CheckOperationReportRoleStrategyDirector roleStrategyDirector = new CheckOperationReportRoleStrategyDirector();
            CheckOperationReportRoleStrategy roleStrategy = roleStrategyDirector.getRoleStrategy(director.getReportAreaType());
            if (!roleStrategy.release(userMaxRole)) {
                throw new SzException(SystemCode.USER_OPERATION_WARNING, "当前角色权限，无法操作报表发布");
            }

            //将报表设置发布
            report.setCurrentStatus(TwoLevelAuditConstant.PUBLIC);

        }else {
            throw new SzException(SystemCode.USER_OPERATION_WARNING, "当前报表状态，不在这个流程操作");
        }

    }
}